import { Controller } from "../../mvc/core/Controller";
import { IRestDoubleController } from "./interfaces/IRestDoubleController";
import { MessageBlock } from "../../../Framework/MQ/MessageBlock";
import { SApplyZazen } from "../../../Message/Public/SApplyZazen";
import { UserPlayer } from "../scene/player/entity/UserPlayer";
import { ThingUtil } from "../scene/layer/utils/ThingUtil";
import { RolePlayer } from "../scene/player/entity/RolePlayer";
import { GameProxy } from "../mvc/GameProxy";
import { EZazenResult } from "../../../Message/Public/EZazenResult";
import { PlayerEvent } from "../scene/events/PlayerEvent";
import { Game } from "../Game";
import { AIManager } from "../scene/ai/AIManager";
import { GameMapUtil } from "../scene/map/GameMapUtil";
import { EMoveStatus } from "../../../Message/Public/EMoveStatus";
import { MsgManager } from "../manager/MsgManager";
import { Language } from "../../../extend/language/Language";
import { DataEvent } from "../events/DataEvent";
import { SZazenReward } from "../../../Message/Game/SZazenReward";
import { ObjCreate } from "../view/common/ObjCreate";
import { PulseSharedObject } from "../view/player/PulseSharedObject";
import { RemainTimeTool } from "../view/award/RemainTimeTool";
import { MsgTipInfo } from "../model/MsgTipInfo";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { SActiveToClient } from "../../../Message/Public/SActiveToClient";
import { IRestSpecialModule } from "../../../modules/interfaces/IRestSpecialModule";
import { RestSpecialModule } from "../../../modules/RestSpecialModule";
import { IRestBriefIntroModule } from "../../../modules/interfaces/IRestBriefIntroModule";
import { RestBriefIntroModule } from "../../../modules/RestBriefIntroModule";
import { IRestSearchPlayerModule } from "../../../modules/interfaces/IRestSearchPlayerModule";
import { RestSearchPlayerModule } from "../../../modules/RestSearchPlayerModule";
import { IRestDiscontinueModule } from "../../../modules/interfaces/IRestDiscontinueModule";
import { RestDiscontinueModule } from "../../../modules/RestDiscontinueModule";
import { RestHintIcon } from "../view/rest/RestHintIcon";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { EPublicCommand } from "../../../Message/Command/EPublicCommand";
import { EGateCommand } from "../../../Message/Command/EGateCommand";
import { ServerCommand } from "../mvc/ServerCommand";
import { EActiveType } from "../../../Message/Public/EActiveType";
import { SearchPlayerData } from "../view/rest/SearchPlayerData";
import { Global } from "../../../com/gengine/global/Global";
import { SEntityId } from "../../../Message/Public/SEntityId";
import { SystemSetter } from "../view/system/SystemSetter";
import { UiHintIconView } from "../view/uiIconBtn/UiHintIconView";
import { RestRequestWindow } from "../view/rest/RestRequestWindow";
type int = number;
//class RestDoubleController
    
    export  class RestDoubleController extends Controller implements IRestDoubleController
    {
        constructor()
        {
            
            super();this._nameInviteArr = [];
            this._applyZazenArr = [];
            return;
        }

        /* internal  */onDoubleZazenCanMoveHandler(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SApplyZazen;
            this.moveToDoubleZazen(loc1);
            return;
        }

        /* internal  */moveToDoubleZazen(arg1: SApplyZazen): void
        {
            var sApplyZazen: SApplyZazen;
            var player: UserPlayer;
            var endPoint: Object /* flash.geom.Point */;
            var onWalkEndHandler: Function;

            var loc1;
            endPoint = null;
            onWalkEndHandler = null;
            sApplyZazen = arg1;
            player = ThingUtil.entityUtil.getEntity(sApplyZazen.toEntityId) as UserPlayer;
            if (player != null) 
            {
                if (RolePlayer.instance.isInDoubleRestRange(player)) 
                {
                    GameProxy.roleProxy.replyZazen(sApplyZazen.applyId, EZazenResult._EZazenResultAgree);
                }
                else 
                {
                    onWalkEndHandler = (arg1: PlayerEvent): void
                    {
                        Game.scene.mouseEnabled = true;
                        AIManager.cancelAll();
                        RolePlayer.instance.stopWalking();
                        GameProxy.roleProxy.replyZazen(sApplyZazen.applyId, EZazenResult._EZazenResultAgree);
                        RolePlayer.instance.removeEventListener(PlayerEvent.WALK_END, onWalkEndHandler);
                        return;
                    }
                    endPoint = GameMapUtil.getTilePoint(player.x, player.y);
                    Game.scene.mouseEnabled = false;
                    RolePlayer.instance.addEventListener(PlayerEvent.WALK_END, onWalkEndHandler);
                    Game.scene.moveRole(endPoint);
                }
            }
            return;
        }

        /* internal  */onAttributeStatusUpdateHandler(arg1: int): void
        {
            if (arg1 != EMoveStatus._EMoveStatusZazen) 
            {
                if (arg1 == EMoveStatus._EMoveStatusDoubleZazen) 
                {
                    MsgManager.addTipText(Language.getString(35201));
                    MsgManager.showRollTipsMsg(Language.getString(35201));
                    if (this._restRequestWindow && this._restRequestWindow.isHide == false) 
                    {
                        this._applyZazenArr = [];
                        this._nameInviteArr = [];
                        this.onRestHintIconClickHandler(null);
                    }
                }
            }
            else 
            {
                MsgManager.addTipText(Language.getString(35200));
                MsgManager.showRollTipsMsg(Language.getString(35200));
            }
            return;
        }

        /* internal  */onRestRefuseAllHandler(arg1: DataEvent): void
        {
            var loc1=null;
            var loc2=0;
            while (loc2 < this._applyZazenArr.length) 
            {
                loc1 = this._applyZazenArr[loc2];
                GameProxy.roleProxy.replyZazen(loc1.applyId, EZazenResult._EZazenResultReject);
                ++loc2;
            }
            this._applyZazenArr = [];
            this._nameInviteArr = [];
            this.onRestHintIconClickHandler(null);
            return;
        }

        /* internal  */onRestDelRequestHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as SApplyZazen;
            var loc2=this._applyZazenArr.indexOf(loc1);
            this._applyZazenArr.splice(loc2, 1);
            this._nameInviteArr.splice(loc2, 1);
            this.onRestHintIconClickHandler(null);
            return;
        }

        /* internal  */onRestOverHandler(arg1: DataEvent): void
        {
            this._applyZazenArr = [];
            this._nameInviteArr = [];
            this.onRestHintIconClickHandler(null);
            if (this._restRequestWindow && this._restRequestWindow.isHide == false) 
            {
                this._restRequestWindow.hide();
            }
            return;
        }

        /* internal  */onFlyRewardInfoHandler(arg1: MessageBlock): void
        {
            var loc2=null;
            var loc1=arg1.messageBase as SZazenReward;
            if (this._text == null) 
            {
                this._text = ObjCreate.createTextField("", 0, 0, 205, 80);
                this._text.multiline = true;
                this._text.wordWrap = true;
            }
            if (!PulseSharedObject.isTodayNotTips("ZazenRewardNotTip")) 
            {
                this._text.htmlText = Language.getStringByParam(35229, RemainTimeTool.getRemainTimeBySeconds(loc1.time), loc1.experienceAdd, loc1.nimbusAdd);
                loc2 = new MsgTipInfo();
                loc2.type = 1;
                loc2.displayObject = this._text;
                loc2.isAllowUpdate = true;
                loc2.notTipEvent = "ZazenRewardNotTip";
                Dispatcher.dispatchEvent(new DataEvent(EventName.MsgTipDateChange, loc2));
            }
            return;
        }

        /* internal  */onZazenSameSexActiveHandler(arg1: SActiveToClient): void
        {
            var loc1=cache.active.isActiveInEffect(arg1.code);
            this.setZazenSameSex(loc1);
            return;
        }

        public get restSpecial(): IRestSpecialModule
        {
            if (this._restSpecial == null) 
            {
                this._restSpecial = new RestSpecialModule();
            }
            return this._restSpecial;
        }

        public get restBriefIntro(): IRestBriefIntroModule
        {
            if (this._restBriefIntro == null) 
            {
                this._restBriefIntro = new RestBriefIntroModule();
            }
            return this._restBriefIntro;
        }

        public get restSearchPlayer(): IRestSearchPlayerModule
        {
            if (this._restSearchPlayer == null) 
            {
                this._restSearchPlayer = new RestSearchPlayerModule();
            }
            return this._restSearchPlayer;
        }

        public get restDiscontinue(): IRestDiscontinueModule
        {
            if (this._restDiscontinue == null) 
            {
                this._restDiscontinue = new RestDiscontinueModule();
            }
            return this._restDiscontinue;
        }

        public get restHintIcon(): RestHintIcon
        {
            if (this._restHintIcon == null) 
            {
                this._restHintIcon = new RestHintIcon();
                this._restHintIcon.addEventListener(flash.events.MouseEvent.CLICK, this.onRestHintIconClickHandler);
            }
            return this._restHintIcon;
        }

        protected /* override */ initServer(): void
        {
            Dispatcher.addEventListener(EventName.Rest_Reiki, this.onRestReikiHandler);
            Dispatcher.addEventListener(EventName.Rest_DoubleRest, this.onRestDoubleRestHandler);
            Dispatcher.addEventListener(EventName.Rest_Profit, this.onRestProfitHandler);
            Dispatcher.addEventListener(EventName.RestIntroOperate, this.onOperateRestIntroHandler);
            Dispatcher.addEventListener(EventName.Rest_Reiki_Begin, this.onBeginRestReikiHandler);
            Dispatcher.addEventListener(EventName.Rest_Reiki_Cancel, this.onCancelRestReikiHandler);
            Dispatcher.addEventListener(EventName.Rest_BeginDoubleRest, this.onBeginDoubleRestHandler);
            Dispatcher.addEventListener(EventName.Rest_RefuseAll, this.onRestRefuseAllHandler);
            Dispatcher.addEventListener(EventName.Rest_DelRequest, this.onRestDelRequestHandler);
            Dispatcher.addEventListener(EventName.Rest_Over, this.onRestOverHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicDoubleZazenInvite, this.onDoubleZazenInviteHandler);
            NetDispatcher.addCmdListener(EGateCommand._ECmdGateZazenRewardInfo, this.onBreakOffDoubleRestHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicDoubleZazenCanMove, this.onDoubleZazenCanMoveHandler);
            NetDispatcher.addCmdListener(ServerCommand.PlayerAttributeStatusUpdate, this.onAttributeStatusUpdateHandler);
            NetDispatcher.addCmdListener(EGateCommand._ECmdGateFlyRewardInfo, this.onFlyRewardInfoHandler);
            NetDispatcher.addCmdListener(ServerCommand.ZazenSameSexActiveCommand, this.onZazenSameSexActiveHandler);
            NetDispatcher.addCmdListener(ServerCommand.ActiveListChange, this.onActiveListChangeHandler);
            RolePlayer.instance.addEventListener(PlayerEvent.DoubleRest, this.onDoubleRestHandler);
            return;
        }

        /* internal  */onDoubleRestHandler(arg1: PlayerEvent): void
        {
            var loc1=arg1.player as UserPlayer;
            if (loc1.isDoubleRest) 
            {
                this.restBriefIntro.hide();
                this.restSearchPlayer.hide();
            }
            return;
        }

        /* internal  */onRestReikiHandler(arg1: DataEvent): void
        {
            this.restSpecial.show();
            return;
        }

        /* internal  */onActiveListChangeHandler(arg1: Object): void
        {
            var loc1=cache.active.isActiveInEffect(EActiveType._EActiveTypeZazenSameSex);
            this.setZazenSameSex(loc1);
            return;
        }

        /* internal  */setZazenSameSex(arg1: Boolean): void
        {
            SearchPlayerData.isZazenSame = arg1;
            if (this._restSearchPlayer && this._restSearchPlayer.isHide) 
            {
                this._restSearchPlayer.updateSearchPlayerInfo();
            }
            return;
        }

        /* internal  */onRestDoubleRestHandler(arg1: DataEvent): void
        {
            this._introX = Global.stage.stageWidth / 2 - this.restBriefIntro.width;
            this._introY = (Global.stage.stageHeight - this.restBriefIntro.height) / 2;
            var loc1=this._introX + this.restBriefIntro.width;
            var loc2=(Global.stage.stageHeight - this.restSearchPlayer.height) / 2;
            this.restSearchPlayer.show(loc1, loc2);
            return;
        }

        /* internal  */onRestProfitHandler(arg1: DataEvent): void
        {
            return;
        }

        /* internal  */onOperateRestIntroHandler(arg1: DataEvent): void
        {
            if (!this.restSearchPlayer.isHide && this.restBriefIntro.isHide) 
            {
                this.restBriefIntro.show(this._introX, this._introY);
            }
            else 
            {
                this._restBriefIntro.hide();
            }
            return;
        }

        /* internal  */onBeginRestReikiHandler(arg1: DataEvent): void
        {
            GameProxy.roleProxy.setNimbusZazen(true);
            return;
        }

        /* internal  */onCancelRestReikiHandler(arg1: DataEvent): void
        {
            GameProxy.roleProxy.setNimbusZazen(false);
            return;
        }

        /* internal  */onBeginDoubleRestHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as SEntityId;
            GameProxy.roleProxy.applyZazen(loc1);
            return;
        }

        /* internal  */onBreakOffDoubleRestHandler(arg1: MessageBlock): void
        {
            if (!this.restBriefIntro.isHide) 
            {
                this.restBriefIntro.hide();
            }
            if (!this.restSearchPlayer.isHide) 
            {
                this.restSearchPlayer.hide();
            }
            if (!this.restSpecial.isHide) 
            {
                this.restSpecial.hide();
            }
            this.restDiscontinue.show();
            var loc1=arg1.messageBase as SZazenReward;
            this._restDiscontinue.update(loc1);
            return;
        }

        /* internal  */onDoubleZazenInviteHandler(arg1: MessageBlock): void
        {
            var loc2=null;
            var loc3=0;
            var loc1=arg1.messageBase as SApplyZazen;
            if (SystemSetter.localSetter.acceptInviteRest) 
            {
                GameProxy.roleProxy.replyZazen(loc1.applyId, EZazenResult._EZazenResultCannotRecv);
            }
            else 
            {
                loc2 = loc1.playerInfo.name;
                loc3 = 0;
                while (loc3 < this._nameInviteArr.length) 
                {
                    if (this._nameInviteArr[loc3] == loc2) 
                    {
                        return;
                    }
                    ++loc3;
                }
                this._nameInviteArr.push(loc2);
                this._applyZazenArr.push(loc1);
                UiHintIconView.instance.addHintIcon(this.restHintIcon);
                this.restHintIcon.requestArr = this._applyZazenArr;
            }
            return;
        }

        /* internal  */onRestHintIconClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            if (this._restRequestWindow == null) 
            {
                this._restRequestWindow = new RestRequestWindow();
            }
            if (this._applyZazenArr.length > 0) 
            {
                this.restHintIcon.requestArr = this._applyZazenArr;
                this._restRequestWindow.updateDataProvider(this._applyZazenArr);
                this._restRequestWindow.show();
            }
            else 
            {
                if (this._restHintIcon) 
                {
                    this._restHintIcon.removeEventListener(flash.events.MouseEvent.CLICK, this.onRestHintIconClickHandler);
                    UiHintIconView.instance.removeHintIcon(this._restHintIcon);
                    this._restHintIcon = null;
                }
                if (this._restRequestWindow && !this._restRequestWindow.isHide) 
                {
                    this._restRequestWindow.hide();
                }
            }
            return;
        }

        private /* var */_restSpecial: IRestSpecialModule;

        private /* var */_restBriefIntro: IRestBriefIntroModule;

        private /* var */_restSearchPlayer: IRestSearchPlayerModule;

        private /* var */_restDiscontinue: IRestDiscontinueModule;

        private /* var */_nameInviteArr: Array<any>;

        private /* var */_applyZazenArr: Array<any>;

        private /* var */_restHintIcon: RestHintIcon;

        private /* var */_restRequestWindow: RestRequestWindow;

        private /* var */_introX: int;

        private /* var */_introY: int;

        private /* var */_text: Object /* flash.text.TextField */;
    }
